home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 2 / CU Amiga Magazine's Super CD-ROM 02 (1996)(EMAP Images)(GB)[!][issue 1996-04].iso / magazine / amiga_e / amigae.jan.archive / 000010_crash!informati…en.de!schmidtj_Tue, 4 Jan 94 04:46:52 PST.msg < prev    next >
Text File  |  1994-02-17  |  5KB  |  136 lines

  1. Received: by bkhouse.cts.com (V1.16/Amiga)
  2.     id AA00000; Tue, 4 Jan 94 04:46:52 PST
  3. Received: from tuminfo2.informatik.tu-muenchen.de by crash.cts.com with smtp
  4.     (Smail3.1.28.1 #18) id m0pHAjt-0000SlC; Tue, 4 Jan 94 04:19 PST
  5. Received: from sunbroy14.informatik.tu-muenchen.de ([131.159.0.114])
  6.      by tuminfo2.informatik.tu-muenchen.de
  7.      with SMTP id <57658>; Tue, 4 Jan 1994 13:19:25 +0100
  8. Received: by sunbroy14.informatik.tu-muenchen.de id <8097>; Tue, 4 Jan 1994 13:19:08 +0100
  9. Date:    Tue, 4 Jan 1994 13:19:03 +0100
  10. X-Mailer: ELM [version 2.4 PL6]
  11. Content-Type: text
  12. Content-Length: 3600
  13. Message-Id: <94Jan4.131908met.8097@sunbroy14.informatik.tu-muenchen.de>
  14. From: Juergen Schmidt <schmidtj@informatik.tu-muenchen.de>
  15. To: amigae@bkhouse.cts.com
  16. Subject: E-string-lists
  17.  
  18. This is another example for lists of strings in E.The special thing about this example
  19. is the use of the function insert().This function inserts a string in a sorted list,
  20. so that it remains sorted.So,starting from NIL,simple calling insert() again and again
  21. will create a sorted list of strings.This is quite handy if you do not want to sort the
  22. list after it has been build.The whole example is in the Public Domain,so use it and change
  23. it at will.Have Fun!
  24.  
  25.  
  26. /* Demo of sorted lists of strings,using the
  27.    procedures insert() and contains()  */
  28.  
  29. /* include module 'utility' for use of Stricmp() */
  30.  
  31. MODULE 'utility'
  32.  
  33. ENUM ER_NONE,ER_MEM,ER_LIB,ER_BREAK,ER_DUP
  34.  
  35. /* Used Exceptions */
  36. /* If opening of library fails,it is indicated by raising ER_LIB and the user is told so */
  37.    
  38. RAISE ER_MEM IF String()=NIL,    
  39.       ER_MEM IF List()=NIL,
  40.       ER_LIB IF OpenLibrary()=NIL,
  41.       ER_BREAK IF CtrlC()=TRUE
  42.        
  43. DEF in[20]:STRING,newlist=NIL
  44.  
  45. PROC main() HANDLE
  46. utilitybase := OpenLibrary('utility.library',37)
  47. WriteF('Please enter some strings.Press <RETURN> to end input.\n')
  48.  REPEAT
  49.   ReadStr(stdout,in)
  50.   /* This example won't allow double insertion of a string */
  51.   IF insert({newlist},in) THEN Raise(ER_DUP)
  52.  UNTIL StrCmp(in,'',ALL)
  53.  WriteF('Sorted List :\n')
  54.  writeList(newlist)
  55.  WriteF('\nSpecify element : ') 
  56.  ReadStr(stdout,in)
  57.  WriteF('Specified element is\s available\n',IF contains(newlist,in) THEN '' ELSE ' not')
  58.  DisposeLink(newlist)
  59. EXCEPT
  60.  WriteF('Error: ')
  61.  SELECT exception
  62.   CASE ER_MEM; WriteF('Out of Memory.\n')
  63.   CASE ER_LIB; WriteF('Could not open utility.library V37.\n')
  64.   CASE ER_BREAK; WriteF('User aborted program.\n')
  65.   CASE ER_DUP; WriteF('Duplicate insertion in list.\n')
  66.   DEFAULT; NOP
  67.  ENDSELECT
  68. ENDPROC
  69.  
  70. /* Insert string item into the list thelist,so that the list remains sorted */
  71. /* Procedure will return TRUE if item was already in the list else FALSE */
  72. /* You should raise an exception if you really want a SET of strings and the result is TRUE */
  73. /* The address of the list must be passed */
  74.  
  75. PROC insert(thelist,item)
  76. DEF tail,thestr,done=FALSE,next,prev,theres
  77.  thestr := String(EstrLen(item))
  78.  StrCopy(thestr,item,ALL)
  79.  IF ^thelist = NIL /* the simplest case : list is empty */
  80.     ^thelist := Link(thestr,NIL)
  81.  ELSE    
  82.   tail := ^thelist
  83.   prev := tail
  84.   IF Stricmp(thestr,tail)<0 /* is item is the least element ? */
  85.    ^thelist := Link(thestr,^thelist)
  86.   ELSE    
  87.    /* This is the loop to search the appropriate place for the string */
  88.    REPEAT
  89.     CtrlC()
  90.     theres := Stricmp(thestr,tail)
  91.     IF tail = NIL  /* did we reach the end of the list ? */
  92.         Link(prev,Link(thestr,NIL))
  93.         done := TRUE
  94.     ELSEIF theres>0 /* is item greater than current element => step forward */
  95.         prev := tail
  96.     tail := Next(tail)
  97.     ELSEIF theres < 0 /* we have found the right place */
  98.     Link(prev,Link(thestr,tail))
  99.         done := TRUE
  100.     ELSE /* here we have also found the right place but the same string already exists */
  101.      next := Next(tail)
  102.         Link(tail,Link(thestr,next))
  103.         RETURN TRUE
  104.     ENDIF
  105.    UNTIL done
  106.   ENDIF
  107.  ENDIF
  108. ENDPROC FALSE
  109.  
  110. /* Find out if list thelist contains string item */
  111. /* Return value is set accordingly */
  112.  
  113. PROC contains(thelist,item)
  114. DEF first
  115.  first := thelist
  116.  LOOP
  117.     IF first = NIL THEN JUMP c_out
  118.     IF StrCmp(first,item,ALL) THEN RETURN TRUE
  119.     first := Next(first)
  120.  ENDLOOP
  121. c_out:
  122. ENDPROC FALSE
  123.  
  124. /* Write list thelist to stdout */
  125.  
  126. PROC writeList(thelist)
  127. DEF first
  128. first := thelist
  129. LOOP
  130.  CtrlC()
  131.  IF first = NIL THEN JUMP w_out
  132.  WriteF('\s\n',first)
  133.  first := Next(first)
  134. ENDLOOP
  135. w_out:
  136. ENDPROC